package com.sbtr.business.trainingserver.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sbtr.business.trainingserver.entity.TrainingServer;
import com.sbtr.business.trainingserver.mapper.TrainingServerMapper;
import com.sbtr.business.trainingserver.service.ITrainingServerService;
import com.sbtr.open.trainingserver.dto.TrainingServerDto;
import com.sbtr.util.BeanCopyUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 图像训练服务器信息 服务实现类
 * </p>
 *

 */
@Transactional
@Service
public class TrainingServerServiceImpl extends ServiceImpl<TrainingServerMapper, TrainingServer>
		implements ITrainingServerService {

	@Override
 	public TrainingServerDto findById(Long id) {

		TrainingServer server = getById(id);
		if (server != null) {
			return BeanCopyUtils.copyProperties(server, TrainingServerDto.class);
		}
		return null;
	}

	@Override
 	public boolean deleteById(Long id) {
		return removeById(id);
	}

	@Override
 	public List<TrainingServerDto> getList(String status,String serverType,String projectType) {
		List<TrainingServer> servers = null;
		QueryWrapper<TrainingServer> wrapper = new QueryWrapper<>();
		if (TrainingServerDto.isStatusValid(status)) {
			wrapper.eq("status", status);
		}
		if (StringUtils.isNotBlank(serverType)) {
			wrapper.eq("server_type", serverType);
		}

		if (StringUtils.isNotBlank(projectType)) {
			wrapper.eq("project_type", projectType);
		}
		servers = list(wrapper);
		return BeanCopyUtils.copyProperties(servers, TrainingServerDto.class);
	}

	@Override
 	public TrainingServerDto save(TrainingServerDto dto) {
		TrainingServer server = BeanCopyUtils.copyProperties(dto, TrainingServer.class);
		saveOrUpdate(server);
		dto.setId(server.getId());
		return dto;
	}

	@Override
	public List<TrainingServer> listAllPublish() {
		Wrapper<TrainingServer> ew = new QueryWrapper<TrainingServer>();
		((QueryWrapper<TrainingServer>) ew).eq("server_type", TrainingServerDto.SERVER_TYPE_PUBLISH).eq("status",
				TrainingServerDto.TRAINING_SERVER_STATUS_ONLINE);
		return baseMapper.selectList(ew);
	}

	@Override
	public List<TrainingServer> listPublishByProjectTpye(String projectType) {
		// TODO Auto-generated method stub
		Wrapper<TrainingServer> ew = new QueryWrapper<TrainingServer>();
		((QueryWrapper<TrainingServer>) ew).eq("server_type", TrainingServerDto.SERVER_TYPE_PUBLISH)
				.eq("project_type", projectType).eq("status", TrainingServerDto.TRAINING_SERVER_STATUS_ONLINE);
		return baseMapper.selectList(ew);
	}

	@Override
	public List<TrainingServer> listPublishByServerType(String serverType) {
		// TODO Auto-generated method stub
		Wrapper<TrainingServer> ew = new QueryWrapper<TrainingServer>();
		((QueryWrapper<TrainingServer>) ew).eq("server_type", serverType).eq("status",
				TrainingServerDto.TRAINING_SERVER_STATUS_ONLINE);
		return baseMapper.selectList(ew);
	}
}
